#!/bin/bash

sd_eid=$(($1 * 10))
wf_eid=$(($1 * 10 + 2))

# It takes about 6 seconds for setting up MCTP endpoint after slot plugged-in
sleep 10
# Check if we got WF BIC's EID
wf_eid=$(($1 * 10 + 2))
busctl get-property xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp/1/"$wf_eid" xyz.openbmc_project.MCTP.Endpoint EID
status=$?
if [ $status -eq 0 ]; then
    exit 0
fi

count=0
while [ $count -lt 3 ]; do
    # Check WF BIC status
    output=$(pldmtool raw -m "$sd_eid" -d 0x80 0x02 0x3A 0x4c 0xff)
    status=$?
    if [ $status -ne 0 ]; then
        echo "Error executing pldmtool command"
        exit 1
    fi
    # Get data at Rx line
    rx_data=$(echo "$output" | grep "Rx:" | awk '{for (i=3; i<=NF; i++) print $i}')
    # Get last byte
    wf_ready=$(echo "$rx_data" | tail -n 1)
    # 02 means WF BIC ready, otherwise 01.
    if [ "$wf_ready" == "02" ]; then
        busctl call xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp au.com.CodeConstruct.MCTP SetupEndpointByConfigPath s /xyz/openbmc_project/inventory/system/board/Yosemite_4_Wailua_Falls_Slot_"$1"/BIC
        break
    else
        count=$((count + 1))
        sleep 2
    fi
done
